import tkinter as tk
import Util.WindowsUtil as util
import warnings

"""
@author LiuBoyuan

DESCRIPTION
    选择面板控件
        作用：进行选择数据的面板,有标题有信息
    
"""


class SelectPane:
    def __init__(self, background_color="black", font_color='white', method=[], titleList=[], infoList=[], win=None,
                 isBuild=True,btn_value=["<- 上一个","就是这个","下一个 ->"],titleFont=None,subtitleFont=None):
        """
        构造器
        @param background_color: 背景颜色
        @param font_color:  字体颜色
        @param titleList:  标题列表
        @param method 方法列表
        @param infoList:  介绍列表
        @param win:  显示窗口容器
        @param isBuild: 确认自动的创建对象
        @param btn_value: 按钮的文本
        @param titleFont: 标题字体
        @param subtitleFont: 副标题字体
        """


        self.btn_value = btn_value
        self.index = 0  # 当前页面
        self.method = method
        self.result = None  # 被选中的索引
        if len(titleList) != len(infoList):
            raise AttributeError("titleList infoList 列表大小不一致")
        if not win:
            raise AttributeError("没有父容器")
        self.win = win

        self.titleFont = ("华文中宋", 12) if titleFont is None else titleFont
        self.subTitleFont = ("华文中宋",10) if subtitleFont is None else subtitleFont

        self.ListTitle = titleList
        self.ListInfo = infoList
        self.BackColor = background_color
        self.fc = font_color
        self.ContainerFrameManager, self.ContainerFrame = ..., ...
        self.back_ground_label = ...
        if isBuild: self.Builder()

    def Builder(self):
        """
        创建对象
        @param: win 窗口对象
        @return:
        """
        win = self.win
        self.ContainerFrame = tk.LabelFrame(win, fg=self.fc, bg=self.BackColor, borderwidth=0)
        self.TitleLabel = tk.Label(self.ContainerFrame, text="NoThingsToDisplay",
                                   font=self.titleFont, fg=self.fc, bg=self.BackColor)
        tk.Label(self.ContainerFrame, bg=self.BackColor, text=" ").pack()
        self.TitleLabel.pack()

        tk.Label(self.ContainerFrame, bg=self.BackColor, text=" ",font=self.subTitleFont).pack()
        tk.Label(self.ContainerFrame, bg=self.BackColor, text=" ",font=self.subTitleFont).pack()

        self.InfoLabel = tk.Label(self.ContainerFrame, fg=self.fc, bg=self.BackColor,font=self.subTitleFont)
        self.InfoLabel.pack()

        self.ButtonNext = tk.Button(self.ContainerFrame, fg=self.fc, bg=self.BackColor)
        self.ButtonLast = tk.Button(self.ContainerFrame, fg=self.fc, bg=self.BackColor)
        self.ButtonOkay = tk.Button(self.ContainerFrame, fg=self.fc, bg=self.BackColor)
        self.ButtonNext.bind("<Button-1>", lambda e: self.Page(module="DOWN"))
        print(self.index)
        print(self.method)
        try:
            self.ButtonOkay.bind("<Button-1>", self.method[self.index])
        except:
            self.ButtonOkay.bind("<Button-1>", self.method)

        self.ButtonLast.bind("<Button-1>", lambda e: self.Page(module="UP"))

        self.ButtonNext['text'] = self.btn_value[0]
        self.ButtonNext.place(rely=0.8, relx=0.04, relwidth=0.3)
        self.ButtonOkay['text'] = self.btn_value[1]
        self.ButtonOkay.place(rely=0.8, relx=0.36, relwidth=0.3)
        self.ButtonLast['text'] = self.btn_value[2]
        self.ButtonLast.place(rely=0.8, relx=0.68, relwidth=0.3)
        self.Page(FixPage=0)  # 默认第一页

    def getContainer(self):
        """
        获取Label Frame容器
        @return:
        """
        return self.ContainerFrame

    def show(self, x=-1, y=-1, relx=-1, rely=-1, w=500, h=500, relw=0, relh=0):
        """
        该方法已被废弃，请勿使用
        @param win: 父窗口容器
        @param x: X
        @param y: Y

        """
        warnings.warn("推荐使用getContainer函数动态设置位置")

        if relx == -1 or rely == -1:
            self.ContainerFrame.place(x=0, y=0, width=w, height=h)
            print("任意放置")
        elif x == -1 or y == -1:
            print("百分比放置")
            self.ContainerFrame.place(relx=relx, rely=rely)

        self.ShowIndex()

    def getSelect(self):
        """
        获取当前选中的面板
        @return:
        """
        return self.result if self.result != None else -1

    def Page(self, module="UP", FixPage=None):
        """
        翻页
        @param module:
        @return:
        """

        if FixPage is None:
            if module == "UP":
                if self.index + 1 != len(self.ListInfo):
                    self.index += 1
                else:
                    self.index = 0
            elif module == "DOWN":
                if self.index - 1 != -1:
                    self.index -= 1
                else:
                    self.index = len(self.ListInfo) - 1
            try:
                self.ButtonOkay.bind("<Button-1>", self.method[self.index])
            except:
                self.ButtonOkay.bind("<Button-1>", self.method)

        else:
            self.index = FixPage

        self.ShowIndex()

    def ShowIndex(self):
        """
        展示当前的文本
        @return:
        """
        self.TitleLabel['text'] = self.ListTitle[self.index]
        self.InfoLabel['text'] = self.ListInfo[self.index]
        self.win.update()


if __name__ == '__main__':
    win = tk.Tk()
    win.geometry("500x500")
    tk.Label(win, text=" ").pack()

    tk.Label(win, text="zxc ").pack()
    m = [
        lambda e: print('这是信息1'),
        lambda e: print('这是信息2'),
        lambda e: print('这是信息3'),
    ]
    myLabel = SelectPane(background_color="pink", font_color='black', titleList=["这是标题", "这是标题2", "这是标题3"],
                         infoList=["这是信息", "这是信息2", "这是信息3"], win=win, method=m)

    label = myLabel.getContainer()
    label.place(relx=0, rely=0, relwidth=1, relheight=1)
    # label.pack()

    # myLabel.show(0, 0)
    win.mainloop()
